<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ModelRelation.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//class Permission extends Baogg_Db_Table {
class RelationInModel  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	
	function __construct() {
		$this->_name =   'relation_in_model';
		
		$this->myPrimary= $this->_primary='relation_in_model_id';
		parent::__construct();
	}	
	
	
	
	/**
	 * 
	 * copy field from relations.model_pid to current model
	 * @param int $relation_in_model_id
	 * @return boolean
	 */
	function copyField($relation_in_model_id){
		global $LANG;
		 
		 
		$flag=true;
		 

		$ModelField=new ModelField();
		$Model=new Model();
		$ModelRelation=new ModelRelation();
		 
		if(!$relation_in_model_id){
			return false;
		}
		$rs_relation_in_model=$this->getOne($relation_in_model_id);
		if(!@$rs_relation_in_model[0]['is_select'] || !@$rs_relation_in_model[0]['state']){
			return true;
		}
		$rs_model_relation=$ModelRelation->getOne($rs_relation_in_model[0]['model_relation_id']);
		$rs_p_model=$Model->getOne($rs_model_relation[0]['model_pid']);
		try{
			$main_table=$this->_db->query("desc ".$this->db_prefix."{$rs_p_model[0]['table_name']}")->fetchAll();
		}catch(Exception $e){
			return false;
		}
		 
		Baogg_Language::loadFile($rs_p_model[0]['table_name']);
    	foreach($main_table as $v){
    		 //primary key must use in ext table
    		$field=array( 
				'model_id'=>$rs_relation_in_model[0]['model_id'], 
				'field_name'=>$v['Field'], 
				'show_name'=>BAOGG_LANGUAGE::get($v['Field']), 
				'field_type'=>$v['Type'], 
				'field_options'=>'', 
				'tips'=>'', 
				'check_pattern'=>'', 
				'check_error'=>'', 
				'model_pid'=>$rs_model_relation[0]['model_pid'],  
				'is_basic'=>1, 
				'state'=>1, 
				'ordering'=>0,
    			'init_value'=>is_null($v['Default'])?'':$v['Default'],
    			'field_null'=>is_null($v['Null'])?'YES':$v['Null'],//$v['Null']
    			'field_key'=>is_null($v['Key'])?'':$v['Key'],//$v['Key'],
    			'field_extra'=>is_null($v['Extra'])?'':$v['Extra']//$v['Extra'],
    			//'init_value'=>$v['Default'], 
	    		//'field_null'=>$v['Null'], 
	    		//'field_key'=>$v['Key'], 
	    		//'field_extra'=>'' 
    		);
    		
    		$existField=$ModelField->getList(array('model_id'=>$rs_relation_in_model[0]['model_id'],'model_pid'=>$rs_model_relation[0]['model_pid'],'field_name'=>$v['Field']));
    		if(!$existField){
    			$_flag=$ModelField->_insert($field);
    			$flag=$flag && $_flag;  
    		}
    		  		
    	}
    	
    	return $flag;
	}
	
	
	function autoloadRelation($model_id=0){
		$flag=true;
		
		$model_id=(int)$model_id;
		
		$ModelRelation=new ModelRelation();
		$rs_model_relation=$ModelRelation->getList(array('model_id'=>$model_id));
		
		foreach((array)$rs_model_relation as $v){
			if(!$this->getList(array('model_id'=>$model_id,'model_relation_id'=>$v['model_relation_id']))){
				$flag=$this->_insert($v) && $flag;
			}
		}
		return $flag;
	}
	
	
	
}
